home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / deluge / component.py < prev    next >
Text File  |  2009-06-16  |  7KB  |  234 lines

  1. #
  2. # component.py
  3. #
  4. # Copyright (C) 2007, 2008 Andrew Resch <andrewresch@gmail.com>
  5. #
  6. # Deluge is free software.
  7. #
  8. # You may redistribute it and/or modify it under the terms of the
  9. # GNU General Public License, as published by the Free Software
  10. # Foundation; either version 3 of the License, or (at your option)
  11. # any later version.
  12. #
  13. # deluge is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. # See the GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with deluge.    If not, write to:
  20. #     The Free Software Foundation, Inc.,
  21. #     51 Franklin Street, Fifth Floor
  22. #     Boston, MA  02110-1301, USA.
  23. #
  24. #    In addition, as a special exception, the copyright holders give
  25. #    permission to link the code of portions of this program with the OpenSSL
  26. #    library.
  27. #    You must obey the GNU General Public License in all respects for all of
  28. #    the code used other than OpenSSL. If you modify file(s) with this
  29. #    exception, you may extend this exception to your version of the file(s),
  30. #    but you are not obligated to do so. If you do not wish to do so, delete
  31. #    this exception statement from your version. If you delete this exception
  32. #    statement from all source files in the program, then also delete it here.
  33. #
  34.  
  35. #
  36.  
  37.  
  38. import gobject
  39. from deluge.log import LOG as log
  40.  
  41. COMPONENT_STATE = [
  42.     "Stopped",
  43.     "Started",
  44.     "Paused"
  45. ]
  46.  
  47. class Component:
  48.     def __init__(self, name, interval=1000, depend=None):
  49.         # Register with the ComponentRegistry
  50.         register(name, self, depend)
  51.         self._interval = interval
  52.         self._timer = None
  53.         self._state = COMPONENT_STATE.index("Stopped")
  54.  
  55.     def get_state(self):
  56.         return self._state
  57.  
  58.     def start(self):
  59.         pass
  60.  
  61.     def _start(self):
  62.         self._state = COMPONENT_STATE.index("Started")
  63.         if self._update():
  64.             self._timer = gobject.timeout_add(self._interval, self._update)
  65.  
  66.     def stop(self):
  67.         pass
  68.  
  69.     def _stop(self):
  70.         self._state = COMPONENT_STATE.index("Stopped")
  71.         try:
  72.             gobject.source_remove(self._timer)
  73.         except:
  74.             pass
  75.  
  76.     def _pause(self):
  77.         self._state = COMPONENT_STATE.index("Paused")
  78.         try:
  79.             gobject.source_remove(self._timer)
  80.         except:
  81.             pass
  82.  
  83.     def _resume(self):
  84.         self._start()
  85.  
  86.     def shutdown(self):
  87.         pass
  88.  
  89.     def _update(self):
  90.         try:
  91.             self.update()
  92.         except AttributeError:
  93.             # This will stop the timer since the component doesn't have an
  94.             # update method.
  95.             return False
  96.         return True
  97.  
  98.  
  99. class ComponentRegistry:
  100.     def __init__(self):
  101.         self.components = {}
  102.         self.depend = {}
  103.  
  104.     def register(self, name, obj, depend):
  105.         """Registers a component.. depend must be list or None"""
  106.         log.debug("Registered %s with ComponentRegistry..", name)
  107.         self.components[name] = obj
  108.         if depend != None:
  109.             self.depend[name] = depend
  110.  
  111.     def get(self, name):
  112.         """Returns a reference to the component 'name'"""
  113.         return self.components[name]
  114.  
  115.     def start(self):
  116.         """Starts all components"""
  117.         for component in self.components.keys():
  118.             self.start_component(component)
  119.  
  120.     def start_component(self, name):
  121.         """Starts a component"""
  122.         # Check to see if this component has any dependencies
  123.         if self.depend.has_key(name):
  124.             for depend in self.depend[name]:
  125.                 self.start_component(depend)
  126.         # Only start if the component is stopped.
  127.         if self.components[name].get_state() == \
  128.             COMPONENT_STATE.index("Stopped"):
  129.             log.debug("Starting component %s..", name)
  130.             self.components[name].start()
  131.             self.components[name]._start()
  132.  
  133.     def stop(self):
  134.         """Stops all components"""
  135.         for component in self.components.keys():
  136.             self.stop_component(component)
  137.  
  138.     def stop_component(self, component):
  139.         if self.components[component].get_state() != \
  140.                 COMPONENT_STATE.index("Stopped"):
  141.             log.debug("Stopping component %s..", component)
  142.             self.components[component].stop()
  143.             self.components[component]._stop()
  144.  
  145.     def pause(self):
  146.         """Pauses all components.  Stops calling update()"""
  147.         for component in self.components.keys():
  148.             self.pause_component(component)
  149.  
  150.     def pause_component(self, component):
  151.         if self.components[component].get_state() not in \
  152.             [COMPONENT_STATE.index("Paused"), COMPONENT_STATE.index("Stopped")]:
  153.             log.debug("Pausing component %s..", component)
  154.             self.components[component]._pause()
  155.  
  156.     def resume(self):
  157.         """Resumes all components.  Starts calling update()"""
  158.         for component in self.components.keys():
  159.             self.resume_component(component)
  160.  
  161.     def resume_component(self, component):
  162.         if self.components[component].get_state() == COMPONENT_STATE.index("Paused"):
  163.             log.debug("Resuming component %s..", component)
  164.             self.components[component]._resume()
  165.  
  166.     def update(self):
  167.         """Updates all components"""
  168.         for component in self.components.keys():
  169.             # Only update the component if it's started
  170.             if self.components[component].get_state() == \
  171.                 COMPONENT_STATE.index("Started"):
  172.                 self.components[component].update()
  173.  
  174.         return True
  175.  
  176.     def shutdown(self):
  177.         """Shuts down all components.  This should be called when the program
  178.         exits so that components can do any necessary clean-up."""
  179.         # Stop all components first
  180.         self.stop()
  181.         for component in self.components.keys():
  182.             log.debug("Shutting down component %s..", component)
  183.             try:
  184.                 self.components[component].shutdown()
  185.             except Exception, e:
  186.                 log.debug("Unable to call shutdown(): %s", e)
  187.  
  188.  
  189. _ComponentRegistry = ComponentRegistry()
  190.  
  191. def register(name, obj, depend=None):
  192.     """Registers a component with the registry"""
  193.     _ComponentRegistry.register(name, obj, depend)
  194.  
  195. def start(component=None):
  196.     """Starts all components"""
  197.     if component == None:
  198.         _ComponentRegistry.start()
  199.     else:
  200.         _ComponentRegistry.start_component(component)
  201.  
  202. def stop(component=None):
  203.     """Stops all or specified components"""
  204.     if component == None:
  205.         _ComponentRegistry.stop()
  206.     else:
  207.         _ComponentRegistry.stop_component(component)
  208.  
  209. def pause(component=None):
  210.     """Pauses all or specificed components"""
  211.     if component == None:
  212.         _ComponentRegistry.pause()
  213.     else:
  214.         _ComponentRegistry.pause_component(component)
  215.  
  216. def resume(component=None):
  217.     """Resumes all or specificed components"""
  218.     if component == None:
  219.         _ComponentRegistry.resume()
  220.     else:
  221.         _ComponentRegistry.resume_component(component)
  222.  
  223. def update():
  224.     """Updates all components"""
  225.     _ComponentRegistry.update()
  226.  
  227. def shutdown():
  228.     """Shutdowns all components"""
  229.     _ComponentRegistry.shutdown()
  230.  
  231. def get(component):
  232.     """Return a reference to the component"""
  233.     return _ComponentRegistry.get(component)
  234.